<template>
    <div class="shop-car">
        <div class="bar">
            <van-nav-bar title="购物车" left-text="返回" left-arrow @click-left="onClickLeft" />
        </div>
        <div class="shop-list">
            <ul>
                <li ref="divDom" v-for="(item, i) in toplist.data">
                    <div class="shop-box">
                        <span class="iconfont icon-a-duizhengquedagou" ref="btn"></span>
                        <div class="shop-img">
                            <img :src="item.img" alt="" srcset="">
                        </div>
                        <div class="shop-title">
                            <h4>{{ item.name }}</h4>

                            <p>{{ item.cup }}/{{ item.temp }}/{{ item.sugar }}</p>
                        </div>
                    </div>
                    <div class="shop-price">
                        <h5 style="display: flex;">共计: <h4>￥{{ item.price }}</h4>
                        </h5>
                    </div>
                </li>
            </ul>
        </div>
        <div class="shop-total">
            <div class="total-price">
                <p>应付合计：</p>
                <h4 style="display: flex; align-items: center;">￥<h3>21</h3>
                </h4>
            </div>
            <div class="total-end">
                <h4 @click="getShow">去结算</h4>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ref, reactive, getCurrentInstance, onMounted, onUpdated, inject, watch } from 'vue';
import { carList, removeList, removeCar, getOrderList } from '../request'
import { useRoute, useRouter } from 'vue-router';
import { showSuccessToast, showFailToast } from 'vant';
import store from '@/store';

const onClickLeft = () => history.back();

let index: any = ref(0);

let isShow = ref(false);

let toplist: any = reactive({
    data: []
})

const route = useRoute();

const bigCar = () => {
    carList().then((content: any) => {
        let { result } = content;
        if (result.length == 0) {
            showFailToast('购物车空荡荡');
        }
        let hot = result.map((item: any) => {
            let obj: any = {};
            obj.id = item.car_id;
            obj.name = item.car_name;
            obj.img = item.car_img;
            obj.cup = item.car_cup;
            obj.sugar = item.car_sugar;
            obj.temp = item.car_temp;
            obj.price = item.car_total
            return obj
        })
        toplist.data = hot;
        // store.commit('setAAA', hot);

    })
        .catch(err => {
            console.log(err);
        })
}
bigCar()

onUpdated(() => {
    const ccc: any = document.querySelectorAll('.shop-box span');
    const li: any = document.querySelectorAll('.shop-list li');
    const h4: any = document.querySelectorAll('.shop-price h4');
    const h3: any = document.querySelector('.total-price h3');

    let sum = 0
    for (let i = 0; i < ccc.length; i++) {
        ccc[i].addEventListener('click', () => {
            // let shopId = route.query.shopId;
            showSuccessToast(`删除成功`);
            removeList({ car_id: toplist.data[i].id }).then((content: any) => {
                bigCar()
            })
                .catch(err => {
                    console.log(err);
                })
            // 
        })

        sum += Number(h4[i].innerHTML.slice(1))
    }
    h3.innerHTML = sum;


})
let orderRandom = ref('');

function sjs(leng: any) {
    // 大写字母、小写字母、数字能出现的情况
    var char = '0123456789'
    // 定义一个字符串接收随机生成的字符串
    var chars = ''
    // 遍历
    for (var i = 0; i < leng; i++) {
        //    chars 的值为char中下标为随机数的值
        // 这个随机数取值是Math.random()  0-1不到1
        // Math.random()*char.length 0-1之间的数*char的长度，生成一个0-char.length之间的数,取整不包含最后一个，但是length长度为最大下标+1，所以不用加一
        //最后用+来拼接
        chars += char.charAt((Math.random() * (char.length)))
    }
    //返回得到的字符串
    return chars
}

orderRandom.value = sjs(15)

let count = ref(0);
let allName = ref('');
let total = ref(0)
const getShow = () => {
    showSuccessToast(`结算成功`);

    isShow.value = true
    if (toplist.data.length == 0) {
        return
    }

    count.value = Number(toplist.data.length);
    toplist.data.forEach((element: any) => {
        total.value += Number(element.price);
        allName.value += element.name + `，`
    })
    console.log(store.state.total);


    getOrderList({ ord_id: orderRandom.value, ord_name: allName.value, ord_address: store.state.addressDetail, ord_count: count.value, ord_total: total.value })
        .then((content: any) => {
            console.log(content);
        }).catch(error => {
            console.log(error);
        })

    removeCar().then((content: any) => {
        bigCar()
    }).catch(error => {
        console.log(error);
    })

}


</script>

<style lang="scss" >
body {
    .shop-car {
        position: relative;
        width: 100%;
        height: 566px;
        background-color: #f8f8f8;
        overflow-y: scroll;

        .kong {
            width: 100%;
            color: #c0c0c0;
            margin: 12rem 6rem;
        }

        .shop-list {
            width: 100%;
            // height: 100%;
            background-color: #fff;

            ul {
                width: 100%;
                margin: 0 auto;

                li {
                    width: 95%;
                    height: 4.5rem;
                    display: flex;
                    justify-content: space-between;
                    margin: .1rem auto 1rem;
                    border-top: 1px solid #ccc;
                    box-shadow: 0 2px 3px 1px rgba(0, 0, 0, 0.12);
                    overflow: hidden;
                    border-radius: 8px;

                    .active {
                        display: none;
                    }

                    .shop-box {
                        display: flex;
                        margin-left: 1rem;

                        span {
                            color: rgb(86, 193, 235);
                            font-size: 20px;
                            line-height: 4.5rem;
                        }

                        .shop-img {
                            width: 4.5rem;
                            height: 100%;

                            img {
                                width: 100%;
                                height: 100%;
                            }
                        }

                        .shop-title {
                            h4 {
                                margin-top: 1.1rem;
                                font-size: 14px;
                            }

                            p {
                                margin-top: .4rem;
                                font-size: 11px;
                            }
                        }
                    }

                    .shop-price {
                        margin-right: 1rem;
                        line-height: 4.5rem;

                        h5 {
                            font-size: 13.5px;
                            color: #2b4c7e;

                            h4 {
                                font-size: 15px;
                                color: #575454;
                            }
                        }
                    }

                }
            }
        }

        .shop-total {
            background-color: #fff;
            position: fixed;
            bottom: 48px;
            left: 0;
            width: 100%;
            height: 3.3rem;
            display: flex;
            align-items: center;
            justify-content: space-between;


            .total-price {
                display: flex;
                width: 9rem;
                align-items: center;
                margin-left: .8rem;

                p {
                    font-size: 13px;
                }
            }

            .total-end {
                width: 6rem;
                height: 100%;
                background-color: #90c0ef;
                color: #fff;
                line-height: 3.3rem;
                text-align: center;
                font-size: 14px;
            }
        }
    }
}
</style>